package web1.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import web1.bean.user;

/**
 * Servlet implementation class addservlet
 */
@WebServlet("/addservlet")
public class addservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addservlet() {
        super();
        // TODO Auto-generated constructor stub
    }
//    public void init() throws ServletException{
//    	try {  
//            Class.forName("com.mysql.jdbc.Driver");  
//        } catch (ClassNotFoundException e) {  
//            e.printStackTrace();  
//        }  
//    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
//		 Connection conn = null;  
//	        PreparedStatement ps = null;  
//	        ResultSet rs = null;  
		request.setCharacterEncoding("utf-8");
		String url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false";
		String username="root";
		String password="Root123@";
		Connection conn=null;
	        try {  
//	            List<user> userList = new ArrayList<>(); 
	        	try {
					Class.forName("com.mysql.jdbc.Driver");
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
	            conn = DriverManager.getConnection(url,"root","Root123@");  
	            PreparedStatement ps = conn.prepareStatement("insert into user(id,name,age,sex) values(?,?,?,?)");  
	            int id=Integer.valueOf(request.getParameter("id")).intValue();
	            int age=Integer.valueOf(request.getParameter("age")).intValue();
	            ps.setInt(1, id);         
	            ps.setString(2, request.getParameter("name"));
	            ps.setInt(3, age);
//	            ps.setString(3, request.getParameter("age"));
	            ps.setString(4, request.getParameter("sex"));
	            ps.execute();
	            ps.close();
	            conn.close();
//	            rs = ps.executeQuery();  
//	            while(rs.next()){  
//	            	int id=rs.getInt("id");
//	                String name = rs.getString("userName"); 
//	                int age=rs.getInt("age");
//	                String sex = rs.getString("sex");  
//	                user u = new user(id,name, age,sex);  
//	                userList.add(u);  
//	            }  
//	            request.setAttribute("userList", userList);  
//	            request.getRequestDispatcher("insert.jsp").forward(request, response);  
	        } catch (SQLException e) {  
	            e.printStackTrace();  
	        }
//	        finally {  
//	            try {  
//	               
//	                if(ps!=null){  
//	                    ps.close();  
//	                }  
//	                if(conn!=null){  
//	                    conn.close();  
//	                }  
//	            } catch (SQLException e) {  
//	                e.printStackTrace();  
//	            }  
//	        }  
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
